//
//  LSIndexProgressView.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/10/23.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSIndexProgressView.h"

@interface LSIndexProgressView ()

@property (nonatomic,strong) UILabel *titleLabel;

@end

@implementation LSIndexProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

- (void)setUI {
    //背景图
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(10,(self.frame.size.height-8)/2,kWidth-10-44,8)];
    [self addSubview:backGroundView];
    backGroundView.backgroundColor = [UIColor colorWithHex:0xeeeeee];
    backGroundView.layer.cornerRadius = 4;
    backGroundView.layer.masksToBounds = YES;
    //进度
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(0,4)];
    [bezierPath addLineToPoint:CGPointMake((kWidth-10-44)/20,4)];
    [bezierPath closePath];

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithHex:0xfbca30].CGColor, (__bridge id)[UIColor colorWithHex:0xfda61a].CGColor, (__bridge id)[UIColor colorWithHex:0xff8908].CGColor];
    gradientLayer.locations = @[@0.3, @0.5, @1.0];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = CGRectMake(0,0,(kWidth-10-44)/20,8);
    gradientLayer.cornerRadius = 4;
    
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.path = bezierPath.CGPath;
    progressLayer.lineWidth = 8;
    progressLayer.lineJoin = kCALineJoinRound;
    progressLayer.lineCap = kCALineCapRound;
    progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    progressLayer.strokeColor = [[UIColor colorWithHex:0xff8908] CGColor];
    
    gradientLayer.mask = progressLayer;
    [backGroundView.layer addSublayer:gradientLayer];

    CABasicAnimation * baseAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    baseAnimation.duration = 2;
    baseAnimation.fromValue = @0.0;
    baseAnimation.toValue = @1.0;
    baseAnimation.repeatCount = 1;
    [progressLayer addAnimation:baseAnimation forKey:@"strokeAnimation"];

}

@end
